package weather.com.weather.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import weather.com.weather.R
import weather.com.weather.bean.WeatherBean

/**
 *  @Auther     于亿鑫
 *  @CreateTime 2018/6/14  9:24
 */
class FutureWeatherAdapter(context: Context,data: List<WeatherBean.Result.Future>) : BaseAdapter() {

    var context = context
    var data = data

    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {

        Log.d("adapter测试数据",data[p0].toString())
        //TODO(测试数据是否正确，查看数据头)

        var holder: ViewHolder
        var v: View = LayoutInflater.from(context).inflate(R.layout.future_weather_item, p2, false)

        if (p1 == null) {
            holder = ViewHolder(v)
            v.tag = holder
        }else {
            v = p1
            holder = v.tag as ViewHolder
        }

        //日期格式转换
        var mon:String
        var day:String
        if (data[p0].date.substring(4,6).toInt()<10) mon = data[p0].date.substring(5,6)
        else mon = data[p0].date.substring(4,6)

        if (data[p0].date.substring(6,8).toInt()<10) day = data[p0].date.substring(7,8)
        else day = data[p0].date.substring(6,8)

        var datt = "${mon}-${day}"
        var wee = "周${data[p0].week.substring(2)}"

        holder.date.text = datt
        holder.dateWeek.text = wee
        holder.futureWeather.text = data[p0].weather
        holder.temperatureFuture.text = data[p0].temperature
        return v

    }

    override fun getItem(p0: Int): Any {
        return data[p0]
    }

    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }

    override fun getCount(): Int {
        return data.size
    }

    class ViewHolder(view: View) {
        var date: TextView = view.findViewById(R.id.date)
        var dateWeek:TextView = view.findViewById(R.id.week_date)
        var futureWeather:TextView = view.findViewById(R.id.weather)
        var temperatureFuture:TextView = view.findViewById(R.id.temperature_future)

    }
}